package net.atomarrow.util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
 * 脚本调用工具类
 * @author Michael
 *
 */
public class ScriptUtil {
	 private static ScriptEngine engine =null;
	 public static Object execute(String expression){
		 try {
			String script =" function check(){ return "+expression+"; }";
			getInstance().eval(script); 
		    Invocable inv2 = (Invocable) getInstance(); 
			return inv2.invokeFunction("check");
		} catch (ScriptException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	 }
	 
	 public  static ScriptEngine getInstance(){
		 if(engine==null){
			 ScriptEngineManager factory=new ScriptEngineManager();
			 engine = factory.getEngineByName("JavaScript"); 
		 }
		 return engine;
	 }
	 
}
